﻿namespace ImageP
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// Contrast form
    /// </summary>
    public partial class contrast : Form
    {
        /// <summary>
        /// An object from image
        /// </summary>
        private Image im;

        /// <summary>
        /// An object from class enhancements
        /// </summary>
        private Enhancements enhance = new Enhancements();

        /// <summary>
        /// An object from class read image
        /// </summary>
        private ReadImage rI = new ReadImage();

        /// <summary>
        /// contrast class constructor
        /// </summary>
        /// <param name="image">image</param>
        /// <param name="rI">an object from readImage</param>
        public contrast(Image image, ReadImage rI)
        {
            this.InitializeComponent();
            this.rI = rI;
            this.im = image;
            this.pictureBox1.Image = this.im;          
        }

        /// <summary>
        /// The event handler of the button click
        /// </summary>
        /// <param name="sender">An object of type button</param>
        /// <param name="e">An object that contains all the button events</param>
        private void button1_Click(object sender, EventArgs e)
        {
            Bitmap bm = new Bitmap(this.pictureBox1.Image);
            int newMin = Convert.ToInt32(this.textBox1.Text);
            int newMax = Convert.ToInt32(this.textBox2.Text);
            bm = this.enhance.SetContrast(newMin, newMax, this.pictureBox1.Image);
            this.pictureBox2.Image = bm;   
        }

        /// <summary>
        /// The event handler of the trackBar
        /// </summary>
        /// <param name="sender">An object of type button</param>
        /// <param name="e">An object that contains all the button events</param>
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            Bitmap old = new Bitmap(this.im);

            this.trackBar1.Minimum = -100;
            this.trackBar1.Maximum = 100;
            this.trackBar1.Width = 180;
            this.trackBar1.TickFrequency = 0;
            Bitmap bm = new Bitmap(this.pictureBox1.Image);
            bm = this.enhance.SetBrightness(trackBar1.Value, old);
            this.pictureBox2.Image = bm;
        }

        /// <summary>
        /// Save toolStrip
        /// </summary>
        /// <param name="sender">An object of type button</param>
        /// <param name="e">An object that contains all the button events</param>
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.saveFileDialog1.Filter = "*.JPG|*.JPG|*.GIF|*.GIF|*.PPM|*.PPM|(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.* ";

            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                int index = this.saveFileDialog1.FilterIndex;
                SaveFormats save = new SaveFormats();
                save.CheckFormat(index, this.pictureBox2.Image, this.saveFileDialog1.FileName, this.rI.ImgType, this.rI.Comment, this.rI.Flag);
            }
        }

        /// <summary>
        /// The event handler of the button click
        /// </summary>
        /// <param name="sender">An object of type button</param>
        /// <param name="e">An object that contains all the button events</param>
        private void button2_Click(object sender, EventArgs e)
        {
            Bitmap bm = new Bitmap(this.pictureBox1.Image);
            int gamma = Convert.ToInt32(this.textBox3.Text);
            bm = this.enhance.Gamma(gamma, this.pictureBox1.Image);
            this.pictureBox2.Image = bm;
        }

        /// <summary>
        /// The event handler of the button click
        /// </summary>
        /// <param name="sender">An object of type button</param>
        /// <param name="e">An object that contains all the button events</param>
        private void button3_Click(object sender, EventArgs e)
        {
            Bitmap bm = new Bitmap(this.pictureBox1.Image);
            bm = this.enhance.GreyScale(this.pictureBox1.Image);
            this.pictureBox2.Image = bm;
        }      
    }
}
